package de.szut.ita13sommerfeld.blockadventure.controller;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import de.szut.ita13sommerfeld.blockadventure.gameentities.Entity;
import de.szut.ita13sommerfeld.blockadventure.gameentities.character.Character;

public class KeyInputAction extends KeyAdapter {

	Controller controller;
	
	public KeyInputAction(Controller controller) {
		this.controller = controller;
	}
	
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		
		for(Entity entity : controller.entities) {
			if(entity.getX() >= controller.camera.getViewArea().getMinimumX() && entity.getX() < controller.camera.getViewArea().getMaximumX()) {
				if(entity instanceof Character) {
					Character character = (Character)entity;
					if(key == KeyEvent.VK_D) character.setVelocityX(5);
					if(key == KeyEvent.VK_A) character.setVelocityX(-5);
					if(key == KeyEvent.VK_W && !character.isJumping()) {
						character.setVelocityY(-10);
						character.setJumping(true);	
					}
				}
			}
		}
		
		if(key == KeyEvent.VK_ESCAPE) {
			System.exit(1);
		}
	}
	
	public void keyReleased(KeyEvent e) {
		int key = e.getKeyCode();
		for(Entity entity : controller.entities) {
			if(entity.getX() >= controller.camera.getViewArea().getMinimumX() && entity.getX() < controller.camera.getViewArea().getMaximumX()) {
				if(entity instanceof Character) {
					Character character = (Character)entity;	
					if(key == KeyEvent.VK_D) character.setVelocityX(0);
					if(key == KeyEvent.VK_A) character.setVelocityX(0);
				}
			}
		}	
	}
}
